/**
 * @program: pettyloan
 * @ClassName RepayRecordServiceImpl
 * @author: 汪江
 * @create: 2022-12-06 15:47
 **/
package com.wn.pettyloan.service.impl;

import com.wn.pettyloan.dto.PayDto;
import com.wn.pettyloan.dto.RepayRecordDto;
import com.wn.pettyloan.entity.Repay;
import com.wn.pettyloan.entity.RepayRecord;
import com.wn.pettyloan.mapper.RepayMapper;
import com.wn.pettyloan.mapper.RepayRecordMapper;
import com.wn.pettyloan.po.OverdueInfo;
import com.wn.pettyloan.service.RepayRecordService;
import com.wn.pettyloan.util.Repayment;
import com.wn.pettyloan.util.utilentity.RePay;
import com.wn.pettyloan.util.utilentity.RepayPlan;
import com.wn.pettyloan.vo.RepayRecordVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

@Service
public class RepayRecordServiceImpl implements RepayRecordService {
    @Resource
    private RepayRecordMapper repayRecordMapper;
    @Resource
    private RepayMapper repayMapper;

    @Override
    public List<OverdueInfo> overdueInfo(List list) {
        return repayRecordMapper.overdueInfo(list);
    }
    @Override
    public List<RepayRecordVO> companyRepayRecord(RepayRecordDto repayRecordDto) {
        return repayRecordMapper.companyRepayRecord(repayRecordDto);
    }

    @Override
    public int updateStatus(List list) {
        return repayRecordMapper.updateStatus(list);
    }

    @Override
    public List<RepayRecord> inspectStatus() {
        return repayRecordMapper.inspectStatus();
    }

    //支付成功
    @Override
    @Transactional
    public boolean paySuccess(PayDto payDto) {
        long repayReocrdID = payDto.getRepayReocrdId();//获取本次订单的id
        RepayRecord repayRecord = repayRecordMapper.selectByPrimaryKey(repayReocrdID);
        repayRecord.setPayTypeId(payDto.getPayTypeId());//设置本期还款方式
        repayRecord.setCurrentNowTime(new Date());//修改还款时间
        repayRecord.setRepayRecordStatus(1);//修改还款状态
        int i = repayRecordMapper.updateByPrimaryKeySelective(repayRecord);
        if (i > 0) {
            System.out.println("修改当前还款记录时间和状态成功");
        } else {
            return false;
        }
        Repay repay = repayMapper.selRepayAlready(payDto.getRepayReocrdId());//查还款信息表
        repay.setRepayAlready(repay.getRepayAlready().add(repayRecord.getCurrentPrincipalPayable()));//修改已还款
        int i1 = repayMapper.updateByPrimaryKeySelective(repay);//数据库中修改还款信息
        if (i1 > 0) {
            System.out.println("成功修改当前已还本金");
        } else {
            return false;
        }
        //不是最后一期添加下一期的还款记录
        if (repayRecord.getRepayCurrentPeriod() < repayRecord.getRepayNum()) {
            //获取下个月的还款日期
            RepayRecord repayRecord1 = new RepayRecord();
            Date overdueTime = repayRecord.getOverdueTime();//获取本期还款的截至日期
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(overdueTime);
            calendar.add(Calendar.MONTH, 1);
            Date nextOverdueTime = calendar.getTime();
            //获取还款方式
            String s = repayRecordMapper.selRepayMethodNameById(payDto);
            //设置获取下期还款记录所需信息，还款本金，利息，总期数，当前期数，已还本金
            RepayPlan repayPlan = new RepayPlan();
            repayPlan.setPrincipal(repay.getRepayShould().doubleValue());
            repayPlan.setInterestRate(repay.getCurrentInterest().doubleValue());
            repayPlan.setSumMonth(repayRecord.getRepayNum());
            repayPlan.setNowMonth(repayRecord.getRepayCurrentPeriod());
            repayPlan.setAlreadyRepy(repay.getRepayAlready().doubleValue());
            //获取下期的还款本金，还款利息，总额
            RePay rePay = new RePay();
            if (s.equals("等本等息")) {
                rePay = Repayment.method1(repayPlan);
            } else if (s.equals("等额本息")) {
                rePay = Repayment.method2(repayPlan);
            } else if (s.equals("等额本金")) {
                rePay = Repayment.method3(repayPlan);
            } else if (s.equals("先息后本")) {
                rePay = Repayment.method4(repayPlan);
            }
            //设置下期参数
            repayRecord1.setRepayId(repayRecord.getRepayId());
            repayRecord1.setRepayCurrentPeriod(repayRecord.getRepayCurrentPeriod() + 1);
            repayRecord1.setRepayNum(repayRecord.getRepayNum());
            repayRecord1.setCurrentPrincipalPayable(BigDecimal.valueOf(rePay.getPrincipal()));
            repayRecord1.setCurrentInterestPayable(BigDecimal.valueOf(rePay.getInterest()));
            repayRecord1.setCurrentTotalMoney(BigDecimal.valueOf(rePay.getTotal()));
            repayRecord1.setOverdueTime(nextOverdueTime);
            repayRecord1.setRepayRecordStatus(2);
            //添加下期还款记录
            int i2 = repayRecordMapper.insertSelective(repayRecord1);
            if (i2 > 0) {
                return true;
            } else {
                return false;
            }
        }
        return true;
    }

    @Override
    public String selRepayMethodNameById(PayDto payDto) {
        return repayRecordMapper.selRepayMethodNameById(payDto);
    }

    @Override
    public RepayRecordVO selContractByid(RepayRecordDto repayRecordDto) {
        return repayRecordMapper.selContractByid(repayRecordDto);
    }

    @Override
    public List<RepayRecordVO> selRepayRecordById(RepayRecordDto repayRecordDto) {
        return repayRecordMapper.selRepayRecordById(repayRecordDto);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return repayRecordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(RepayRecord record) {
        return repayRecordMapper.insert(record);
    }

    @Override
    public int insertSelective(RepayRecord record) {
        return repayRecordMapper.insertSelective(record);
    }

    @Override
    public RepayRecord selectByPrimaryKey(Long id) {
        return repayRecordMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(RepayRecord record) {
        return repayRecordMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(RepayRecord record) {
        return repayRecordMapper.updateByPrimaryKey(record);
    }
}